package com.lyf.baoxian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lyf.baoxian.R;

/**
 * Created by 刘亚飞 on 2016/11/30.
 */

public class LoginActivity extends Activity implements View.OnClickListener {
    private EditText et_username;
    private EditText et_password;
    private Button bt_login;
    private Button bt_register;

    //显示密码明文的按钮
    private Button bt_change;

    //是否显示密码
    private boolean showPwd = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    /**
     * 初始化布局
     */
    private void initView() {
        et_username = (EditText) findViewById(R.id.et_username);
        et_password = (EditText) findViewById(R.id.et_password);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_register = (Button) findViewById(R.id.bt_register);
        bt_change = (Button) findViewById(R.id.bt_change);
        bt_change.setVisibility(View.GONE);

        bt_login.setOnClickListener(this);
        bt_register.setOnClickListener(this);
        bt_change.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //登录
            case R.id.bt_login:
                //获取用户名和密码
                String username = et_username.getText().toString().trim();
                String password = et_password.getText().toString().trim();
                if (TextUtils.isEmpty(username)) {
                    Toast.makeText(LoginActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }

                checkInfo(username,password);
                break;

            //注册
            case R.id.bt_register:
            break;

            //密码切换
            case R.id.bt_change:
                Toast.makeText(LoginActivity.this,"点击,",Toast.LENGTH_SHORT).show();
                if (showPwd) {  //不显示明文
                    bt_change.setBackgroundResource(R.drawable.off);

                    et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    showPwd = !showPwd;
                }else { //显示明文
                    bt_change.setBackgroundResource(R.drawable.on);
                    et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    showPwd = !showPwd;
                }
                break;

        }

    }

    /**
     * 登录校验
     * @param username
     * @param password
     */
    private void checkInfo(String username, String password) {
        //此处为访问网络
        if (username.equals("lx") && password.equals("123")) {
            Intent intent = new Intent(LoginActivity.this,MainActivity1.class);
            startActivity(intent);
            finish();

        }else {
            Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
        }
    }
}
